#include "defect_fback.h"
#include "halconcpp/HalconCpp.h"
using namespace HalconCpp;
Defect_Fback::Defect_Fback()
{



}

int Defect_Fback::M_Fb(HObject Img,HObject &OutImg)
{
    HTuple  parm;
    //参数 1  产品定位阈值 80
    parm[0] = m_part.para1;
    //**参数2  黑色缺陷阈值 80
    parm[1] = m_part.para2;
    //**参数3 白色缺陷阈值 40
    parm[2] = m_part.para3;
    //**参数4 缺陷大小 50，100
    parm[3] = m_part.para4;
    // **参数5 水平方向内缩
    parm[4] = m_part.para5;
    //**参数6  垂直方向内缩
    parm[5] = m_part.para6;
    ////**参数7 内部缺陷面积
    //parm[6] = m_part.para7;
    ////**参数8 大崩边面积
    //parm[7] = m_part.para8;
    ////**参数9  是否开启划痕检测
    //parm[8] = m_part.para9;





    // Local iconic variables
	HObject  Rect, ImageReduced;
    HObject  Region, ConnectedRegions, SelectedRegions;
    HObject  RegionTrans, Contours, ImageReduced1, Region1;
    HObject  ConnectedRegions1, SelectedRegions1, RegionErosion1;
    HObject  Contours1, ImageReduced2, EdgeAmplitude;
    HObject  EdgeDirection, Region2, ConnectedRegions2;
    HObject  SelectedRegions2, RegionUnion, ConnectedRegions3;
    HObject  RegionTrans1, RegionErosion, RegionDifference;
    HObject  ImageResult1, ImageResult2, ImageResult3;
    HObject  MultiChannelImage;

    // Local control variables
    HTuple  Number;
    try
    {
		OutImg = Img;
		GenRectangle1(&Rect, m_part2.x1, m_part2.y1, m_part2.x2, m_part2.y2);

        //GenRectangle1(&Rect, 490, 715, 982, 1098.703);
		ReduceDomain(Img, Rect, &ImageReduced);
        Threshold(ImageReduced, &Region, parm[0], 200);
        Connection(Region, &ConnectedRegions);

		ErosionRectangle1(ConnectedRegions, &ConnectedRegions, parm[4], parm[5]);
        Connection(ConnectedRegions, &ConnectedRegions);

        SelectShapeStd(ConnectedRegions, &SelectedRegions, "max_area", 70);
        ShapeTrans(SelectedRegions, &RegionTrans, "convex");

        GenContourRegionXld(RegionTrans, &Contours, "border");
        ReduceDomain(ImageReduced, RegionTrans, &ImageReduced1);
        Threshold(ImageReduced1, &Region1, 0, parm[1]);
        Connection(Region1, &ConnectedRegions1);
        SelectShape(ConnectedRegions1, &SelectedRegions1, "area", "and", parm[3], 99999);




		ErosionRectangle1(RegionTrans, &RegionErosion1, parm[4] + 3, parm[5]+3);
        GenContourRegionXld(RegionErosion1, &Contours1, "border");
        ReduceDomain(ImageReduced, RegionErosion1, &ImageReduced2);

        SobelDir(ImageReduced2, &EdgeAmplitude, &EdgeDirection, "sum_abs", 3);

        Threshold(EdgeAmplitude, &Region2, parm[2], 200);
        Connection(Region2, &ConnectedRegions2);
        SelectShape(ConnectedRegions2, &SelectedRegions2, "area", "and", parm[3], 99999);
        Union2(SelectedRegions1, SelectedRegions2, &RegionUnion);
        CountObj(RegionUnion, &Number);

        if (0 != (Number>0))
        {
            Connection(RegionUnion, &ConnectedRegions3);
            ShapeTrans(ConnectedRegions3, &RegionTrans1, "outer_circle");
            ErosionCircle(RegionTrans1, &RegionErosion, 3.5);
            Difference(RegionTrans1, RegionErosion, &RegionDifference);
            PaintRegion(RegionDifference, Img, &ImageResult1, 255, "fill");
            PaintRegion(RegionDifference, Img, &ImageResult2, 0, "fill");
            PaintRegion(RegionDifference, Img, &ImageResult3, 0, "fill");
			Compose3(ImageResult1, ImageResult2, ImageResult3, &OutImg);
			return 1;
        }
		return 0;
    }
    catch(HException ex)
    {
        return -1;
    }
   
}

